package com.bookmall.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bookmall.common.utils.MyQuery;
import com.bookmall.common.utils.PageUtils;
import com.bookmall.member.domain.MemberInfo;
import com.bookmall.member.domain.MemberLevel;
import com.bookmall.member.service.IMemberInfoService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-01-9:52
 */
@RestController
@RequestMapping("/member/info")
public class MemberInfoController extends BaseController {
    @Autowired
    private IMemberInfoService memberInfoService;

    /**
     * 查询会员信息列表
     */
    @PreAuthorize("@ss.hasPermi('member:info:list')")
    @GetMapping("/list")
    public AjaxResult list(MemberInfo memberInfo)
    {
        IPage<MemberInfo> page = memberInfoService.page(new MyQuery<MemberInfo>().getPage(memberInfo), new QueryWrapper<>(memberInfo));
        PageUtils pageUtils = new PageUtils(page);
        return AjaxResult.success(pageUtils);
    }

    /**
     * 导出会员信息列表
     */
    @PreAuthorize("@ss.hasPermi('member:info:export')")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberInfo memberInfo)
    {
        List<MemberInfo> list = memberInfoService.list();
        ExcelUtil<MemberInfo> util = new ExcelUtil<MemberInfo>(MemberInfo.class);
        util.exportExcel(response, list, "会员信息数据");
    }

    /**
     * 获取会员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(memberInfoService.getById(id));
    }

    /**
     * 通过学生学号查询会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:info:query')")
    @GetMapping(value = "/stu/{stuId}")
    public AjaxResult getInfoByStuId(@PathVariable("stuId") String stuId)
    {
        MemberInfo memberInfo = memberInfoService.selectMemberInfoByStuId(stuId);
        if(memberInfo == null){
            return AjaxResult.error();
        }
        return AjaxResult.success(memberInfo);
    }

    /**
     * 新增会员信息
     */
    @PreAuthorize("@ss.hasPermi('member:info:add')")
    @Log(title = "会员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberInfo memberInfo)
    {
        return toAjax(memberInfoService.saveMember(memberInfo));
    }

    /**
     * 修改会员信息
     */
    @PreAuthorize("@ss.hasPermi('member:info:edit')")
    @Log(title = "会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberInfo memberInfo)
    {
        return toAjax(memberInfoService.updateMember(memberInfo));
    }

    /**
     * 删除会员信息
     */
    @PreAuthorize("@ss.hasPermi('member:info:remove')")
    @Log(title = "会员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(memberInfoService.removeBatchByIds(ids));
    }
}
